package com.noreply.parser.handle;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @author
 * @description
 * @datetime 2021/8/30 9:45
 */
public class UserBeanDefinition implements BeanDefinitionParser {
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        RootBeanDefinition def = new RootBeanDefinition();
        def.setBeanClassName("com.noreply.parser.entity.UserXtd");
        //def.setInitMethodName("start");
        //def.setDestroyMethodName("shutdown");

        String id = element.getAttribute("id");

        // 获取 userName 标签属性值
        String name = element.getAttribute("userName");
        // 获取 emailAddress 标签属性值
        String address = element.getAttribute("emailAddress");

        MutablePropertyValues pvs = def.getPropertyValues();
        if (StringUtils.hasText(name)) {
            pvs.addPropertyValue("userName", name);
        }
        if (StringUtils.hasText(address)) {
            pvs.addPropertyValue("emailAddress", address);
        }
        parserContext.getRegistry().registerBeanDefinition(id, def);
        return def;
    }
}
